package com.yanqu.road.dao.impl.activity.daomu.pvp;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.daomu.pvp.DaoMuPvpRoomData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DaoMuPvpRoomDataDaoImpl extends TempDataObjectDao<DaoMuPvpRoomData> {

    public static String getDaoMuPvpRoomDataSql = "select * from `t_c_activity_daomu_pvp_room_data` where `activityId`=?";

    public Map<Integer, DaoMuPvpRoomData> getDaoMuPvpRoomDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DaoMuPvpRoomData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDaoMuPvpRoomDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DaoMuPvpRoomData roomData = parse(rs);
                map.put(roomData.getRoomId(), roomData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertDaoMuPvpRoomDataSql = "insert into `t_c_activity_daomu_pvp_room_data`(activityId,roomId,floor,collapse,shrinkStartTime) values (?,?,?,?,?)";

    @Override
    protected boolean addData(DaoMuPvpRoomData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoomId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isCollapse()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getShrinkStartTime()));
        if (getDb().executeNoneQuery(insertDaoMuPvpRoomDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDaoMuPvpRoomDataSql = "update `t_c_activity_daomu_pvp_room_data` set floor=?,collapse=?,shrinkStartTime=? where activityId= ? and roomId = ? ";

    @Override
    protected boolean updateData(DaoMuPvpRoomData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isCollapse()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getShrinkStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoomId()));
        if (getDb().executeNoneQuery(updateDaoMuPvpRoomDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DaoMuPvpRoomData parseData(ResultSet rs) throws SQLException {
        DaoMuPvpRoomData data = new DaoMuPvpRoomData();
        data.setActivityId(rs.getInt("activityId"));
        data.setRoomId(rs.getInt("roomId"));
        data.setFloor(rs.getInt("floor"));
        data.setCollapse(rs.getBoolean("collapse"));
        data.setShrinkStartTime(rs.getLong("shrinkStartTime"));
        return data;
    }
}
